package com.example.beangenerate.service;

import com.example.beangenerate.dao.MybatisDao;
import com.example.beangenerate.model.Column;
import com.example.beangenerate.model.Table;
import com.example.beangenerate.util.GenerateFile;
import com.example.beangenerate.util.ModelChange;
import com.sun.istack.internal.NotNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class GenerateBeanAndMapper {
    @Resource
    private MybatisDao mybatisDao;
    @Resource
    private ModelChange modelChange;
    @Resource
    private GenerateFile generateFile;

    @ResponseBody
    @PostMapping(value = "/generate/table", produces = "application/json;charset=UTF-8")
    public Table gerateFile(@RequestBody Table table) {
        table.setTableName(table.getTableName().toUpperCase());
        table = mybatisDao.selectOne(table);
        table.setTableName(table.getTableName());
        table.setName(modelChange.changeModel(table.getTableName(),true));
        table.setTableComment(getComment("TABLE",table.getTableName(),""));
        Column column = new Column();
        column.setTableName(table.getTableName());
        List<Column> columnList = mybatisDao.selectList(column);
        columnList.forEach(e->{
            e.setColumnComment(getComment("COLUMN",e.getTableName(),e.getColumnName()));
            e.setTaleColumn(e.getColumnName());
            e.setColumnName(modelChange.changeModel(e.getColumnName(),false));
            e.setDataType(e.getDataType().toUpperCase());
            if("VARCHAR2 VARCHAR CHAR varchar char".contains(e.getDataType())){
                e.setDataType("VARCHAR");
                e.setType("String");
            }
            if("DATE date".contains(e.getDataType())){
                e.setType("Date");
            }
            if("int INT number NUMBER".contains(e.getDataType())){
                e.setDataType("INTEGER");
                e.setType("Integer");
            }
        });
        //mysql获取主键方法
        //List<Column> primaryKeyList = columnList.stream().filter(e->e.getColumnKey().equals("PRI")).collect(Collectors.toList());
        //oracle获取主键方法
        List<Column> primaryKeyList = mybatisDao.selectList(Column.class.getName().concat(".getPrimary"),column);
        //定义要替换的内容参数 map的key为和模版中${}内的值一致 value为要替换的结果
        Map<String, Object> param = new HashMap<>();
        param.put("mapperPackage", "com.example.beangenerate.model");
        param.put("className",table.getTableName());
        param.put("columnList",columnList);
        param.put("primaryKeyList",primaryKeyList);
        param.put("table",table);
        param.put("date",new Date());

        String name = modelChange.changeModel(table.getTableName(),true);
        generateFile.generateFile("templates/sqlmap.txt",param,name.concat("Mapper.xml"));
        generateFile.generateFile("templates/dbBean.txt",param,name.concat(".java"));
        return table;
    }

    /*获取表名中文 字段中文*/
    public String getComment(String type, @NotNull String tableName, String columnName){
        Column column = new Column();
        column.setTableName(tableName);
        String comment = "";
        if(type.equals("TABLE")){
            comment = mybatisDao.selectList(Column.class.getName().concat(".getTableComment"),column).get(0).getColumnComment();
        }else if(type.equals("COLUMN")){
            column.setColumnName(columnName);
            comment = mybatisDao.selectList(Column.class.getName().concat(".getColumnComment"),column).get(0).getColumnComment();
        }
        return comment;
    }
}
